import 'package:flutter/material.dart';
import './ProductEdit.dart';
import '../Models/product.dart';
import '../ScopedModels/ScopedProduct.dart';
import 'package:scoped_model/scoped_model.dart';

class ProductList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ScopedModelDescendant(
        builder: (BuildContext context, Widget child, ScopedProduct model) {
      return ListView.builder(
        itemBuilder: _itemCell,
        itemCount: model.products.length,
      );
    });
  }

  Widget _itemCell(BuildContext context, int index) {
    return ScopedModelDescendant(
        builder: (BuildContext context, Widget child, ScopedProduct model) {
      return Column(
        children: <Widget>[
          ListTile(
            leading:
                CircleAvatar(backgroundImage: AssetImage('assets/food.jpg')),
            title: Text(model.products[index].title),
            subtitle: Text('\$${model.products[index].price}'),
            trailing: IconButton(
              icon: Icon(Icons.edit),
              onPressed: () {
                model.selectProductIndex(index);
                Navigator.push(context,
                    MaterialPageRoute(builder: (context) => ProductEdit()));
              },
            ),
          ),
          Divider()
        ],
      );
    });
  }
}
